package com.hw.one.peach.core.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hw.one.core.auth.context.SystemContextHolder;
import com.hw.one.core.result.MsgResult;
import com.hw.one.core.service.PageService;
import com.hw.one.peach.core.dao.IEncircledAreaDao;
import com.hw.one.peach.core.dao.IMapPointDao;
import com.hw.one.peach.core.service.IEncircledAreaService;
import com.hw.one.util.DateTimeUtil;
import com.hw.one.util.UUIDUtil;

/**
  * @author  sjd 
  * @date 创建时间：2017年7月28日 下午3:12:20 
  * @version 1.0.0
  */
@Service(value = "encircledAreaService")
public class EncircledAreaService extends PageService implements IEncircledAreaService {

	@Resource
	private IEncircledAreaDao encircledAreaDao;
	
	@Resource
	private IMapPointDao mapPointDao;
	
	@Resource
	private IMapPointService mapPointService;
	
	private Logger log = LoggerFactory.getLogger(EncircledAreaService.class);
	
	@Override
	public List<Map<String, Object>> findEncircledAreaInfos(Map<String, Object> map) {
		
		return encircledAreaDao.findEncircledAreaInfos(map);
	}

	@Override
	public Map<String, Object> findEncircledAreaBySiteCode(Map<String, Object> map) {
		log.info( "findEncircledAreaBySiteCode is called,time is {}" , new Date().toString());
		return encircledAreaDao.findEncircledAreaBySiteCode(map);
	}

	@Override
	public MsgResult deletes(Map<String, Object> map) {
		log.info( "deletes{} is called,time is {}" , map, new Date().toString());
		String idStr = (String) map.get("ids");
		if (StringUtils.isBlank(idStr)) {
			return MsgResult.nullResult("请选择要处理的数据!");
		}
		String[] ids = idStr.split(",");
		//Map<String,Object> area;
		for(int i=0;i<ids.length;i++){
			Map<String,Object>	area = encircledAreaDao.getById(ids[i]);
			mapPointDao.deletes(area);	
		}
		// 条件封装
		Map<String, Object> idSMap = new HashMap<>();
		idSMap.put("ids", ids);
		encircledAreaDao.deletes(idSMap);
		return MsgResult.okResult();
	}

	@Override
	public MsgResult add(Map<String, Object> map) {/*
		String nationalSite = (String)map.get("nationalSite");//国内空气站
		String siteCode = (String)map.get("siteCode");
		String areaCode = (String)map.get("areaCode");*/
		map.put("id", UUIDUtil.getUUID());
		//map.put("areaCode", areaCode);
		map.put("creator", SystemContextHolder.getAccountId());
		map.put("createDate", DateTimeUtil.getCurrentDate());
		map.put("editor", SystemContextHolder.getAccountId());
		map.put("modifiedDate", DateTimeUtil.getCurrentDate());
		encircledAreaDao.add(map);    //添加完合围区，下面开始批量添加点位
		
		
		//添加子表
		mapPointService.add(map);
		return MsgResult.okResult();
       /* List<Map<String,Object>> pointList = batchAddPoint(map);	
        if(pointList.size()>0){
        	Map<String,Object> paramMap = new HashMap<String,Object>();
    		paramMap.put("pointList", pointList);
    		mapPointService.add(paramMap);
			return MsgResult.okResult();
        }else{
        	return MsgResult.exceptionResult("添加点位数据有问题");
        }*/
        
		
		
	}

	@Override
	public MsgResult edit(Map<String, Object> map) {
		map.put("editor", SystemContextHolder.getAccountId());
		map.put("modifiedDate", DateTimeUtil.getCurrentDate());
		encircledAreaDao.edit(map);
		mapPointService.add(map);
		
		return MsgResult.okResult();
	}

	@Override
	public MsgResult getById(String id) {
		
		Map<String, Object> encircledAreaMap = encircledAreaDao.getById(id);
		Map<String,Object> paramMap = new HashMap<>();
		String areaCode = (String)encircledAreaMap.get("areaCode");
		if(!StringUtils.isBlank(areaCode)){
			paramMap.put("areaCode", areaCode);
		}
		List<Map<String,Object>> mapPoints = new ArrayList<>();
		List<Map<String,Object>> mapPointList = mapPointService.retrievePointInfos(paramMap);
		for(int i=0;i<mapPointList.size();i++){
			Map<String,Object> point = mapPointList.get(i);
			String pointId = (String)point.get("id");
			point.put("pointId", pointId);
			point.remove("id");
			mapPoints.add(point);
		}
		encircledAreaMap.put("mapPoints", mapPoints);
		return MsgResult.okResult(encircledAreaMap);
	}
		
}
